package com.alm.token.ui.viewpagefragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.alm.token.ConStant;
import com.alm.token.R;
import com.alm.token.base.BaseAdapter;
import com.alm.token.base.BaseViewPageFragment;
import com.alm.token.common.RefreshLoadMoreManager;
import com.alm.token.ui.activity.BlockWalletLoginActivity;
import com.alm.token.ui.activity.MessageDetailActivity;
import com.alm.token.ui.activity.PayPwdActivity;
import com.alm.token.ui.data.BaseEntity;
import com.alm.token.ui.data.bean.MessageData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.MessageNoticePageView;
import com.alm.token.utils.CommonUtil;
import com.chad.library.adapter.base.BaseViewHolder;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class MessageNoticePageFrag extends BaseViewPageFragment implements MessageNoticePageView<MessageData> {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.tv_clickreload)
    TextView tvClickreload;
    @BindView(R.id.nodatashow_body)
    RelativeLayout nodatashowBody;
    private RefreshLoadMoreManager<MessageData.DataBeanX.MsgListBean.DataBean> manager;
    private List<MessageData.DataBeanX.MsgListBean.DataBean> dataBeans = new ArrayList<>();
    private String messageType = "1";

    public static MessageNoticePageFrag newInstance() {
        MessageNoticePageFrag fragment = new MessageNoticePageFrag();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected MyPreenter initPreenter() {
        return preenter = new MyPreenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.viewpage_fragment_message_notice;
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void initViews(View view) {
        initManager();
        swipeRefreshLayout.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {

    }

    private void initManager() {
        MessageAdapter adapter = new MessageAdapter(getActivity());
        manager = new RefreshLoadMoreManager.Builder(getActivity())
                .setAdapter(adapter)
                .setRecyclerView(recyclerView)
                .setOnListItemClickListener(new RefreshLoadMoreManager.OnListItemClickListener() {
                    @Override
                    public void onListItemClick(BaseAdapter.BaseItem item, int position) {
                        if (dataBeans != null && dataBeans.size() != 0) {
                            Intent intent1 = new Intent(getContext(), MessageDetailActivity.class);
                            intent1.putExtra("intentType", 2);
                            intent1.putExtra("msgdataBeansBean", dataBeans.get(position));
                            startActivity(intent1);
                        }
                    }
                })
                .setSwipeRefreshLayout(swipeRefreshLayout)
                .setOnRefreshListener(new RefreshLoadMoreManager.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
//                        showToast("onRefresh");
                        swipeRefreshLayout.setRefreshing(false);
                        preenter.getMessageData(messageType);
                    }
                })
                .build();
    }

    @Override
    protected void loadData() {
        preenter.getMessageData(messageType);

    }

    @Override
    public void onResume() {
        super.onResume();

    }
    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            getActivity().sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            getActivity().finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(getActivity(), PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType", PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            getActivity().finish();
            showToSetPaypswDialog();
        }else if (staue == 666) {
            showToUpdata();
        }
    }
    @Override
    public void getData(MessageData messageData) {

        if (messageData.getStatus() == 0) {
//            showToast(messageData.getMsg());
            if (messageData.getData() != null && messageData.getData().getMsgList() != null
                    && messageData.getData().getMsgList().getData() != null) {//说明有消息
                dataBeans = messageData.getData().getMsgList().getData();
                for (int i = 0; i < dataBeans.size(); i++) {
                    dataBeans.get(i).setLayoutId(MessageAdapter.LAYOUT_ID);
                }
                manager.setData(dataBeans);
            }
            if (dataBeans.size() == 0) {
                swipeRefreshLayout.setVisibility(View.GONE);
                nodatashowBody.setVisibility(View.VISIBLE);
            } else {
                swipeRefreshLayout.setVisibility(View.VISIBLE);
                nodatashowBody.setVisibility(View.GONE);
            }
//            MessageData.DataBeanX.MsgListBean.DataBean dataBean = new MessageData.DataBeanX.MsgListBean.DataBean();
//            dataBean.setTitle("第一条公告标题");
//            dataBean.setContent("第一条公告内容");
//            dataBean.setCreateTime(1544069892000L);
//            dataBean.setLayoutId(MessageAdapter.LAYOUT_ID);
//            dataBeans.add(dataBean);
//
//            dataBean = new MessageData.DataBeanX.MsgListBean.DataBean();
//            dataBean.setTitle("第二条公告标题");
//            dataBean.setContent("第二条公告内容");
//            dataBean.setCreateTime(1549065892000L);
//            dataBean.setLayoutId(MessageAdapter.LAYOUT_ID);
//            dataBeans.add(dataBean);
//            manager.setData(dataBeans);
        }else showToast(messageData.getMsg());
    }

    @Override
    public void onError(String error) {

        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {

        showWaitDialog();
    }

    @Override
    public void hideProgress() {

        hideWaitDialog();
    }

    @OnClick(R.id.tv_clickreload)
    public void onViewClicked() {
        preenter.getMessageData(messageType);
    }

    public class MessageAdapter extends BaseAdapter<MessageData.DataBeanX.MsgListBean.DataBean> {

        public static final int LAYOUT_ID = R.layout.item_message_notice;

        public MessageAdapter(@NonNull Context context) {
            super(context);
        }

        @Override
        protected void convert(BaseViewHolder helper, MessageData.DataBeanX.MsgListBean.DataBean item, int itemType) {

            helper.setText(R.id.title, item.getTitle());
            helper.setText(R.id.date, CommonUtil.getDateTimeFromMillisecond(item.getCreateTime()).split(" ")[0]);
            helper.setText(R.id.message, item.getContent());

//            helper.setText(R.id.title, "借款成功");
//            helper.setText(R.id.date, "2017-05-24");
//            helper.setText(R.id.message, "文字文字文字文字文字文字文字文字文字文字文字文字文字文字");
        }

    }
}
